package com.lcz.authority.admin.mapper;

import com.lcz.authority.common.core.mybatis.BaseMapperX;
import com.lcz.authority.common.core.mybatis.MPJLambdaWrapperX;
import com.lcz.authority.common.domain.entity.SysDeptEntity;
import com.lcz.authority.common.domain.entity.SysRoleDeptEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author lcz
 * @desc 部门管理 数据层
 * @mail lcz_0130@163.com
 * @date 2024/06/17
 */
@Mapper
public interface SysDeptMapper extends BaseMapperX<SysDeptEntity> {
    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    default List<SysDeptEntity> selectDeptList(SysDeptEntity dept) {
        //<select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
        //        <include refid="selectDeptVo"/>
        //        where d.del_flag = '0'
        //		<if test="deptId != null and deptId != 0">
        //			AND dept_id = #{deptId}
        //		</if>
        //        <if test="parentId != null and parentId != 0">
        //			AND parent_id = #{parentId}
        //		</if>
        //		<if test="deptName != null and deptName != ''">
        //			AND dept_name like concat('%', #{deptName}, '%')
        //		</if>
        //		<if test="status != null and status != ''">
        //			AND status = #{status}
        //		</if>
        //		<!-- 数据范围过滤 -->
        //		${params.dataScope}
        //		order by d.parent_id, d.order_num
        //    </select>
        return selectList(new MPJLambdaWrapperX<SysDeptEntity>().selectAll(SysDeptEntity.class)
//                .eq(SysDeptEntity::getDelFlag, "0")
                .eqIfPresent(SysDeptEntity::getDeptId, dept.getDeptId())
                .eqIfPresent(SysDeptEntity::getParentId, dept.getParentId())
                .likeIfPresent(SysDeptEntity::getDeptName, dept.getDeptName())
                .eqIfPresent(SysDeptEntity::getStatus, dept.getStatus())
                .orderByAsc(SysDeptEntity::getParentId, SysDeptEntity::getOrderNum));
    }

    /**
     * 根据角色ID查询部门树信息
     *
     * @param roleId            角色ID
     * @param deptCheckStrictly 部门树选择项是否关联显示
     * @return 选中部门列表
     */
    default List<Long> selectDeptListByRoleId(@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly) {
        //select d.dept_id
        //		from sys_dept d
        //            left join sys_role_dept rd on d.dept_id = rd.dept_id
        //        where rd.role_id = #{roleId}
        //            <if test="deptCheckStrictly">
        //              and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
        //            </if>
        //		order by d.parent_id, d.order_num
        MPJLambdaWrapperX<SysDeptEntity> selectDeptListByRoleIdWrapper = new MPJLambdaWrapperX<>();
        selectDeptListByRoleIdWrapper.select(SysDeptEntity::getDeptId)
                .leftJoin(SysRoleDeptEntity.class, SysRoleDeptEntity::getDeptId, SysDeptEntity::getDeptId)
                .eq(SysRoleDeptEntity::getRoleId, roleId);
        if (deptCheckStrictly) {
            selectDeptListByRoleIdWrapper.notIn(SysDeptEntity::getDeptId, new MPJLambdaWrapperX<SysDeptEntity>().select(SysDeptEntity::getParentId)
                    .leftJoin(SysRoleDeptEntity.class, SysRoleDeptEntity::getDeptId, SysDeptEntity::getDeptId)
                    .eq(SysRoleDeptEntity::getRoleId, roleId));
        }
        selectDeptListByRoleIdWrapper.orderByAsc(SysDeptEntity::getParentId, SysDeptEntity::getOrderNum);
        return selectObjs(selectDeptListByRoleIdWrapper);

    }

    /**
     * 根据部门ID查询信息
     *
     * @param deptId 部门ID
     * @return 部门信息
     */
    default SysDeptEntity selectDeptById(Long deptId) {
        //select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status,
        //			(select dept_name from sys_dept where dept_id = d.parent_id) parent_name
        //		from sys_dept d
        //		where d.dept_id = #{deptId}
        return selectOne(new MPJLambdaWrapperX<SysDeptEntity>().selectAll(SysDeptEntity.class)
                .eq(SysDeptEntity::getDeptId, deptId));
    }

    /**
     * 根据ID查询所有子部门
     *
     * @param deptId 部门ID
     * @return 部门列表
     */
    default List<SysDeptEntity> selectChildrenDeptById(Long deptId) {
        //		select * from sys_dept where find_in_set(#{deptId}, ancestors)
        return selectList(new MPJLambdaWrapperX<SysDeptEntity>().eq(SysDeptEntity::getAncestors, deptId));
    }

    /**
     * 根据ID查询所有子部门（正常状态）
     *
     * @param deptId 部门ID
     * @return 子部门数
     */
    default int selectNormalChildrenDeptById(Long deptId) {
        //		select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)
        return Math.toIntExact(selectCount(new MPJLambdaWrapperX<SysDeptEntity>().eq(SysDeptEntity::getStatus, "0")
//                .eq(SysDeptEntity::getDelFlag, "0")
                .eq(SysDeptEntity::getAncestors, deptId)));
    }

    /**
     * 是否存在子节点
     *
     * @param deptId 部门ID
     * @return 结果
     */
    default int hasChildByDeptId(Long deptId) {
        //select count(1) from sys_dept
        //		where del_flag = '0' and parent_id = #{deptId} limit 1
        return Math.toIntExact(selectCount(new MPJLambdaWrapperX<SysDeptEntity>()
//                .eq(SysDeptEntity::getDelFlag, "0")
                .eq(SysDeptEntity::getParentId, deptId)));
    }

    /**
     * 校验部门名称是否唯一
     *
     * @param deptName 部门名称
     * @param parentId 父部门ID
     * @return 结果
     */
    default SysDeptEntity checkDeptNameUnique(@Param("deptName") String deptName, @Param("parentId") Long parentId) {
        // <include refid="selectDeptVo"/>
        //		where dept_name=#{deptName} and parent_id = #{parentId} and del_flag = '0' limit 1
        return selectOne(new MPJLambdaWrapperX<SysDeptEntity>().eq(SysDeptEntity::getDeptName, deptName)
                        .eq(SysDeptEntity::getParentId, parentId)
//                .eq(SysDeptEntity::getDelFlag, "0")
        );
    }

    /**
     * 新增部门信息
     *
     * @param dept 部门信息
     * @return 结果
     */
    default int insertDept(SysDeptEntity dept) {
        //insert into sys_dept(
        // 			<if test="deptId != null and deptId != 0">dept_id,</if>
        // 			<if test="parentId != null and parentId != 0">parent_id,</if>
        // 			<if test="deptName != null and deptName != ''">dept_name,</if>
        // 			<if test="ancestors != null and ancestors != ''">ancestors,</if>
        // 			<if test="orderNum != null">order_num,</if>
        // 			<if test="leader != null and leader != ''">leader,</if>
        // 			<if test="phone != null and phone != ''">phone,</if>
        // 			<if test="email != null and email != ''">email,</if>
        // 			<if test="status != null">status,</if>
        // 			<if test="createBy != null and createBy != ''">create_by,</if>
        // 			create_time
        // 		)values(
        // 			<if test="deptId != null and deptId != 0">#{deptId},</if>
        // 			<if test="parentId != null and parentId != 0">#{parentId},</if>
        // 			<if test="deptName != null and deptName != ''">#{deptName},</if>
        // 			<if test="ancestors != null and ancestors != ''">#{ancestors},</if>
        // 			<if test="orderNum != null">#{orderNum},</if>
        // 			<if test="leader != null and leader != ''">#{leader},</if>
        // 			<if test="phone != null and phone != ''">#{phone},</if>
        // 			<if test="email != null and email != ''">#{email},</if>
        // 			<if test="status != null">#{status},</if>
        // 			<if test="createBy != null and createBy != ''">#{createBy},</if>
        // 			sysdate()
        // 		)
        System.err.println(dept);
        return insert(dept);
    }

    /**
     * 修改部门信息
     *
     * @param dept 部门信息
     * @return 结果
     */
    default int updateDept(SysDeptEntity dept) {
        //	update sys_dept
        // 		<set>
        // 			<if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
        // 			<if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
        // 			<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
        // 			<if test="orderNum != null">order_num = #{orderNum},</if>
        // 			<if test="leader != null">leader = #{leader},</if>
        // 			<if test="phone != null">phone = #{phone},</if>
        // 			<if test="email != null">email = #{email},</if>
        // 			<if test="status != null and status != ''">status = #{status},</if>
        // 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
        // 			update_time = sysdate()
        // 		</set>
        // 		where dept_id = #{deptId}
        return updateById(dept);
    }

    /**
     * 修改所在部门正常状态
     *
     * @param deptIds 部门ID组
     */
    default void updateDeptStatusNormal(Long[] deptIds) {
        // update sys_dept set status = '0' where dept_id in
        // 	    <foreach collection="array" item="deptId" open="(" separator="," close=")">
        //        	#{deptId}
        //        </foreach>

    }

    /**
     * 修改子元素关系
     *
     * @param depts 子元素
     * @return 结果
     */
    default int updateDeptChildren(@Param("depts") List<SysDeptEntity> depts) {
        // update sys_dept set ancestors =
        //	    <foreach collection="depts" item="item" index="index"
        //	        separator=" " open="case dept_id" close="end">
        //	        when #{item.deptId} then #{item.ancestors}
        //	    </foreach>
        //	    where dept_id in
        //	    <foreach collection="depts" item="item" index="index"
        //	        separator="," open="(" close=")">
        //	        #{item.deptId}
        //	    </foreach>
        return 0;
    }

    /**
     * 删除部门管理信息
     *
     * @param deptId 部门ID
     * @return 结果
     */
    default int deleteDeptById(Long deptId) {
        //		update sys_dept set del_flag = '2' where dept_id = #{deptId}
        return deleteById(deptId);
    }
}
